package comm;

import java.util.ArrayList;

public class GraphComm {

    static class Node {

        ArrayList<Integer> nodes = new ArrayList<Integer>();
        ArrayList<Integer> cost = new ArrayList<Integer>();

        public Node() {
        }

        public void add(int n, int c) {
            nodes.add(n);
            cost.add(c);
        }
    };
    private ArrayList<Node> lGraph = new ArrayList<Node>();

    public GraphComm() {
    }

    public void add() {
        lGraph.add(new Node());
    }

    public void addArc(int n, int m, int c) {
        lGraph.get(n).add(m, c);
        lGraph.get(m).add(n, c);
    }

    public ArrayList<Integer> getPath(int n) {
        ArrayList<Integer> path = new ArrayList<Integer>();
        path.add(n);
        int index = n;
        while (path.size() < lGraph.size()) {
            int min = -1;
            for (int i = 0; i < lGraph.get(index).nodes.size(); i++) {
                if (!path.contains(lGraph.get(index).nodes.get(i))) {
                    if (min == -1) {
                        min = i;
                    } else {
                        if (lGraph.get(index).cost.get(i) < lGraph.get(index).cost.get(min)) {
                            min = i;
                        }
                    }
                }
            }
            index = lGraph.get(index).nodes.get(min);
            path.add(index);
        }
        return path;
    }
}